﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Jewelnoid
{
    class SecretCode
    {
        InputController input;
        List<Keys> code;
        Object2D leo;
        Object2D teo;
        Boolean konamiCodeInserted;
        int showDev;
        double time;
       
        public SecretCode()
        {
            this.input = new InputController();
            this.code = new List<Keys>();

            Texture2D leoTexture = Game1.self.Content.Load<Texture2D>("Leo");
            Texture2D teoTexture = Game1.self.Content.Load<Texture2D>("Teo");

            this.leo = new Object2D(leoTexture, new Vector2(-leoTexture.Width, Game1.self.graphics.PreferredBackBufferHeight - leoTexture.Height/2), Color.White);
            this.teo = new Object2D(teoTexture, new Vector2(Game1.self.graphics.PreferredBackBufferWidth + teoTexture.Width/2, Game1.self.graphics.PreferredBackBufferHeight - teoTexture.Height / 2), Color.White);

            this.konamiCodeInserted = false;
            this.showDev = 0;
            this.time = 0;
        }

        public void update(GameTime time)
        {
            getCodeInserted();

            if (this.code.Count == 8)
            {
                this.konamiCodeInserted = isKonamiCode();

                if (this.konamiCodeInserted)
                {
                    Random rand = new Random();
                    SoundManager.playSound("toasty");

                    this.time = time.TotalGameTime.TotalSeconds;
                    this.showDev = rand.Next(1,3);
                }

                this.code.Remove(this.code[0]);
            }

            switch (this.showDev)
            {
                case 1:
                    if (playAnimation(leo, time))
                    {
                        this.showDev = 0;
                        this.konamiCodeInserted = false;
                        this.leo.position.X = -this.leo.texture.Width;
                    }
                    break;
                case 2:
                    if (playAnimation(teo, time))
                    {
                        this.showDev = 0;
                        this.konamiCodeInserted = false;
                        this.teo.position.X = Game1.self.graphics.PreferredBackBufferWidth + this.teo.texture.Width / 2;
                    }
                    break;
            }
        }

        public void draw(SpriteBatch spriteBatch)
        {
            if (this.konamiCodeInserted)
            {
                this.leo.draw(spriteBatch);
                this.teo.draw(spriteBatch);
            }
        }

        public Boolean playAnimation(Object2D obj, GameTime time)
        {
            float startPosition = obj.position.X;
            int speedAnimation = 15;

            if (startPosition < Game1.self.graphics.PreferredBackBufferWidth / 2)
            {
                if (this.time >= time.TotalGameTime.TotalSeconds - 0.75)
                {
                    if (startPosition < obj.texture.Width / 2)
                    {
                        if (obj.position.X + speedAnimation > obj.texture.Width / 2)
                        {
                            obj.position.X = obj.texture.Width / 2;
                        }
                        else
                        {
                            obj.position.X += speedAnimation;
                        }
                    }
                }
                else
                {
                    obj.position.X -= speedAnimation;

                    if (obj.position.X +  obj.texture.Width / 2 <= 0)
                    {
                        return true;
                    }
                }
            }
            else
            {
                if (this.time >= time.TotalGameTime.TotalSeconds - 0.75)
                {
                    if (startPosition >= Game1.self.graphics.PreferredBackBufferWidth - obj.texture.Width / 2)
                    {
                        if (obj.position.X - speedAnimation < Game1.self.graphics.PreferredBackBufferWidth - obj.texture.Width / 2)
                        {
                            obj.position.X = Game1.self.graphics.PreferredBackBufferWidth - obj.texture.Width / 2;
                        }
                        else
                        {
                            obj.position.X -= speedAnimation;
                        }
                    }
                }
                else
                {
                    obj.position.X += speedAnimation;

                    if (obj.position.X - obj.texture.Width / 2 >= Game1.self.graphics.PreferredBackBufferWidth)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        private void getCodeInserted()
        {
            this.input.getState();

            if (this.input.isKeyJustPressed(Keys.Down) ||
               (this.input.isMouseLeftButtonJustPressed() && this.input.mouse.Y >= 3 * Game1.self.graphics.PreferredBackBufferHeight / 4))
            {
                this.code.Add(Keys.Down);
            }

            if (this.input.isKeyJustPressed(Keys.Up) ||
               (this.input.isMouseLeftButtonJustPressed() && this.input.mouse.Y <= Game1.self.graphics.PreferredBackBufferHeight / 4))
            {
                this.code.Add(Keys.Up);
            }

            if (this.input.isKeyJustPressed(Keys.Left) ||
               (this.input.isMouseLeftButtonJustPressed() && this.input.mouse.X <= Game1.self.graphics.PreferredBackBufferWidth / 4))
            {
                this.code.Add(Keys.Left);
            }

            if (this.input.isKeyJustPressed(Keys.Right) ||
               (this.input.isMouseLeftButtonJustPressed() && this.input.mouse.X >= 3 * Game1.self.graphics.PreferredBackBufferWidth / 4))
            {
                this.code.Add(Keys.Right);
            }
        }

        private Boolean isKonamiCode()
        {
            return (this.code[0].Equals(Keys.Up) && this.code[1].Equals(Keys.Up) &&
                    this.code[2].Equals(Keys.Down) && this.code[3].Equals(Keys.Down) &&
                    this.code[4].Equals(Keys.Left) && this.code[5].Equals(Keys.Right) &&
                    this.code[6].Equals(Keys.Left) && this.code[7].Equals(Keys.Right));
        }
    }
}
